package com.edu.dgut.service;

import com.edu.dgut.dao.ordertableRepository;
import com.edu.dgut.entity.ordertableEntity;
import com.edu.dgut.entity.ordertable_goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ordertableService {

    @Autowired
    private ordertableRepository ordertable;

    @Autowired
    private salesService sales = new salesService();

    //添加订单，订单号由当前时间加两位随机数组成
    public Map<String,Object> saveOrModify(ordertableEntity orderTable){
        int i = 0;
        List<ordertable_goods> new_ordertable = new ArrayList<>();
        Date date = new Date(); //获取当前时间
        Random random = new Random();//创建一个对象
        StringBuffer ret = new StringBuffer(20);//初始化StringBuffer，字符串拼接
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
        ret.append(format.format(date));//将日期附加到StringBuffer对象的ret后边
        //不带参数的nextInt()会生成所有有效的整数（包含正数，负数，0），abs取绝对值保证随机生成的数都是正数
        String rand = String.valueOf(Math.abs(random.nextInt()));
        // 将生成的2位随机数附加到StringBuffer对象的ret后边，但是日期先附加到ret，所以随机数附加到日期的后边
        ret.append(rand.substring(0, 2));
        orderTable.setOrdertableId(ret.toString());//将拼接完成的对象转为字符串并保存
        //将订单号依次加上数字生成中间表ID
        while(orderTable.getOrdertable_goods().size()>i){
            StringBuffer ret1 = new StringBuffer(20);
            ret1.append(ret);
            ret1.append(i);
            ordertable_goods old_orderTable = orderTable.getOrdertable_goods().get(i++);
            old_orderTable.setId(ret1.toString());
            new_ordertable.add(old_orderTable);
        }
        orderTable.setOrdertable_goods(new_ordertable);
        //订单生成时间
        orderTable.setDate(date);
        if(orderTable.getState() == 1){     //订单已完成，生成订单完成时间
            orderTable.setFinish(date);
        }
        orderTable = ordertable.save(orderTable);
        Map<String,Object> map = new HashMap<String,Object>();
        if(orderTable != null) {
            map.put("status", 1);
            if(orderTable.getState() == 1){     //订单已完成，并将金额加入日销售额和月销售额
                BigDecimal total_price = orderTable.getTotal_price();
                return sales.toDailyAndMonthly(total_price);
            }
        }else {
            map.put("status", 0);
        }
        return map;
    }

    //通过订单号修改订单状态
    public Map<String, Object> ModifyState(String ordertable_id, int state) {
        List<ordertableEntity> old_ordertable = ordertable.findByOrdertableId(ordertable_id);
        ordertableEntity new_ordertable = new ordertableEntity();
        Map<String,Object> map = new HashMap<String,Object>();
        if(old_ordertable != null) {
            old_ordertable.get(0).setState(state);
            if(old_ordertable.get(0).getState() == 1){     //订单已完成，生成订单完成时间
                Date date = new Date(); //获取当前时间
                old_ordertable.get(0).setFinish(date);
            }
            new_ordertable = ordertable.save(old_ordertable.get(0));
            if (new_ordertable != null) {
                if (old_ordertable.get(0).getState() == 1) {     //订单已完成，生成订单完成时间，并将金额加入日销售额和月销售额
                    BigDecimal total_price = old_ordertable.get(0).getTotal_price();
                    return sales.toDailyAndMonthly(total_price);
                }
                map.put("status", 1);
            } else {
                map.put("status", 0);
            }
        }
        return map;
    }
}
